package com.lenovo.agricultureclient.util;

import java.io.IOException;
import java.io.StringReader;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class Util
{
	//获取本地ip地址
	public static String getLocalIpAddress(Context context) throws UnknownHostException 
	{
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		String IP;
		int ipAddress = wifiInfo.getIpAddress();
		IP = intToIp(ipAddress);
		return IP;
	}

	// ip地址转化成字符串
	public static String intToIp(int i) {
		return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)+ "." + ((i >> 24) & 0xFF);
	}
	
	/** 
     * 检测邮箱地址是否合法 
     * @param email 
     * @return true合法 false不合法 
     */  
    public static boolean isEmail(String email)
    {  
    	if (null==email || "".equals(email)) return false;  
    	//Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
    	Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配  
    	Matcher m = p.matcher(email);  
    	return m.matches();  
    }  
    
    /**
	 * 快速封装一个soap协议
	 * @param bodyContent soap协议中的body内容
	 * @return 完整的soap协议
	 */
    public static String getSoapXml(String bodyContent) 
    {
    	  StringBuilder sb = new StringBuilder();
    	  sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    	      + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
    	      + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
    	      + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
    	      + "<soap:Body>"
    	      + bodyContent
    	      + "</soap:Body></soap:Envelope>");
    	  return sb.toString();
    }
    
    public static String getXmlElementStr(String name, String value)
    {
    	return "<"+name+">"+value+"</"+name+">";
    }
    
    /**
     * 从完整的soap协议内容中解析出soap body内容
     * @param soapXml	完整的soap协议
     * @return	soap body内容
     */
    public static Element getSoapBodyElement(String soapXml)
    {
    	Element soapBodyEle = null;
    	//采用doc的方式解析xml内容
    	DocumentBuilderFactory factory = null;
		DocumentBuilder builder = null;
		Document document = null;
		StringReader stringReader = new StringReader(soapXml);
		InputSource inputSource = new InputSource(stringReader);
		
		factory = DocumentBuilderFactory.newInstance();
		try 
		{
			builder = factory.newDocumentBuilder();
			document = builder.parse(inputSource);
			
			Element root = document.getDocumentElement();
			if(root != null)
			{
				//从soap协议中找到soap:Body标签并提取该标签里的内容
				soapBodyEle = (Element)root.getElementsByTagName("soap:Body").item(0);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return soapBodyEle;
    }
    
    //从xml的父标签中提取子标签的内容
    public static String getChildElementValueStr(Element element, String childName)
    {
    	Element childEle = (Element)element.getElementsByTagName(childName).item(0);
		if(childEle!=null){
			return childEle.getFirstChild().getNodeValue();
		}
		return "";
    }
    
    //从xml的父标签中提取子标签的内容，并把内容转成int类型返回
    public static int getChildElementValueInt(Element element, String childName)
    {
    	int ret = 0;
    	String strValue = getChildElementValueStr(element,childName);
    	if(strValue.equals(""))
    	{
    		try {
    			ret = Integer.parseInt(strValue);
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    	return ret;
    }
}
